package cn.kai.basic.service.impl;

import cn.kai.basic.mapper.BaseMapper;
import cn.kai.basic.query.BaseQuery;
import cn.kai.basic.service.IBaseService;
import cn.kai.basic.util.PageList;
import cn.kai.org.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author Kai
 * @Date 2022/6/8 15:33
 * @PackageName:cn.kai.basic.service.impl
 * @ClassName: BaseServiceImpl
 * @Description: service公共实现类
 * @Version 1.0
 */

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> mapper;

    /**
     * 新增数据
     * @param t
     */
    @Override
    @Transactional
    public void save(T t) {
        mapper.save(t);
    }

    /**
     * 修改数据
     * @param t
     */
    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    /**
     * 删除数据
     * @param id
     */
    @Override
    @Transactional
    public void removeById(Long id) {
        mapper.deleteById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<T> queryAll() {
        return mapper.loadAll();
    }

    /**
     * 基于id查询
     * @param id
     * @return
     */
    @Override
    public T queryById(Long id) {
        return mapper.loadById(id);
    }

    /**
     * 高级查询及分页
     * @param query
     * @return
     */
    @Override
    public PageList<T> queryPage(BaseQuery query) {
        Long total = mapper.queryCount(query);

        List<T> rows = mapper.queryData(query);

        return new PageList<T>(total,rows);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void batchRemove(List<Long> ids) {
        mapper.batchRemove(ids);
    }
}
